#!/usr/bin/env python
# -*- coding: utf-8 -*-

# about driver
from selenium import webdriver
from webdriver_helper import get_webdriver

# driver options
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.ie.options import Options as IeOptions
from selenium.webdriver.edge.options import Options as EdgeOptions

# driver service
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.ie.service import Service as IEService

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select

# mouse and keyboard
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys



# element operations ------------------------------------------------------------------------------------------
def locate_element(driver, locator_type: str, locator_value: str, timeout=10):
    if locator_type == "id":
        return WebDriverWait(driver, timeout,0.5).until(EC.presence_of_element_located((By.ID, locator_value)))
    elif locator_type == "name":
        return WebDriverWait(driver, timeout,0.5).until(EC.presence_of_element_located((By.NAME, locator_value)))
    elif locator_type == "xpath":
        return WebDriverWait(driver, timeout,0.5).until(EC.presence_of_element_located((By.XPATH, locator_value)))
    elif locator_type == "link_text":
        return WebDriverWait(driver, timeout,0.5).until(EC.presence_of_element_located((By.LINK_TEXT, locator_value)))
    elif locator_type == "partial_link_text":
        return WebDriverWait(driver, timeout,0.5).until(
            EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, locator_value)))
    elif locator_type == "tag_name":
        return WebDriverWait(driver, timeout,0.5).until(EC.presence_of_element_located((By.TAG_NAME, locator_value)))
    elif locator_type == "class_name":
        return WebDriverWait(driver, timeout,0.5).until(EC.presence_of_element_located((By.CLASS_NAME, locator_value)))
    elif locator_type == "css_selector":
        return WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.CSS_SELECTOR, locator_value)))
    else:
        raise ValueError("Unsupported locator type: {}".format(locator_type))


def click_element(element):
    element.click()


def input_text(element, text: str):
    element.clear()
    element.send_keys(text)

# mouse and keyboard operations -------------------------------------------------------------------------------